/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.campaign.util;

import com.aptech.campaign.biz.ReportBiz;
import java.awt.Color;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

/**
 *
 * @author Tai Nguyen
 */
public class PieStatistic {

    /**
     * create Dataset Fo rSale Of Campaign
     * @param int campaignID
     * @return PieDataset result
     */
    public PieDataset createDatasetForSaleOfCampaign(int campaignID) {
        ReportBiz reBuz = new ReportBiz();
        DefaultPieDataset result = new DefaultPieDataset();

        float saleTarget = reBuz.findBySaleTarget(campaignID);
        float actualSale = reBuz.findByActualSale(campaignID);
        float percentSaleTarget = 0;
        float percentActualSale = 0;
        percentSaleTarget = saleTarget * 100 / (saleTarget + actualSale);
        percentActualSale = 100 - percentSaleTarget;
        //ArrayList<Fee> arr = fee.readFeeInOneYear(2010);
        // for (Fee f : arr) {
        result.setValue("Sale Target", percentSaleTarget);
        result.setValue("Actual Sale", percentActualSale);
        //}
        return result;

    }

    /**
     * create Dataset For Budget Of Campaign
     * @param int campaignID
     * @return PieDataset result
     */
    public PieDataset createDatasetForBudgetOfCampaign(int campaignID) {
        ReportBiz reBuz = new ReportBiz();
        DefaultPieDataset result = new DefaultPieDataset();

        float bugetAllocation = reBuz.findByBudgetAllocation(campaignID);
        float actualBudget = reBuz.findByActualBudget(campaignID);
        float percentBugetAllocation = 0;
        float percentActualBudget = 0;
        percentBugetAllocation = bugetAllocation * 100 / (bugetAllocation + actualBudget);
        percentActualBudget = 100 - percentBugetAllocation;
        //ArrayList<Fee> arr = fee.readFeeInOneYear(2010);
        // for (Fee f : arr) {
        result.setValue("EstimateBugdet", percentBugetAllocation);
        result.setValue("ActualBudget", percentActualBudget);
        //}
        return result;

    }

    /**
     *
     * @param dataset
     * @param title
     * @return chart
     */
    public JFreeChart createChart(PieDataset dataset, String title) {
        JFreeChart chart = ChartFactory.createPieChart(
                title, // chart title
                dataset, // data
                true, // include legend
                true,
                false);

        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setStartAngle(290);
        plot.setSectionPaint(0, Color.BLUE);
        plot.setSectionPaint(1, Color.red);

        plot.setForegroundAlpha(1f);
        plot.setBackgroundPaint(Color.WHITE);
        plot.setSectionOutlinesVisible(true);

        //ignore null and zero value
        plot.setIgnoreZeroValues(true);
        plot.setIgnoreZeroValues(true);

        //plot.setExplodePercent(0, 0.5);


        return chart;

    }
}
